﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Net.Mail;
using System.Net;
using FSY.API.Utilities;
namespace FSY.API.Mail
{
    /// <summary>
    /// Class điểu khiển viecj gửi mail
    /// </summary>
    public class Fsy_MailController
    {
        private static bool IsEmail(string email)
        {
            Regex regex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
            bool result = true;
            String[] ALL_EMAILS = email.Split(';');
            foreach (string emailaddress in ALL_EMAILS)
            {
                result = regex.IsMatch(emailaddress);
                if (result == false)
                    break;
            }
            return result;
        }
        public static string SendMail(Fsy_MailInfo mailInfo)
        {
            try
            {
                Regex regex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
                bool result = true;
                string strMessage = "";
                String[] ALL_EMAILS = mailInfo.MailTo.Split(';');
                foreach (string emailaddress in ALL_EMAILS)
                {
                    result = regex.IsMatch(emailaddress);
                    if (result == false)
                    {
                        strMessage = "Địa chỉ email không hợp lệ.";
                        break;
                    }
                }

                if (result)
                {
                    try
                    {                        
                        MailMessage msg = new MailMessage(mailInfo.MailFrom, mailInfo.MailTo, mailInfo.Subject, mailInfo.BodyMail);                        
                        if (mailInfo.Bcc != null && mailInfo.Bcc != "")
                        {
                            String[] ALL_EMAILS_Bcc = mailInfo.MailTo.Split(';');
                            foreach (string emailaddress in ALL_EMAILS_Bcc)
                            {
                                result = regex.IsMatch(emailaddress);
                                if (result == false)
                                {
                                    strMessage = "Địa chỉ email không hợp lệ. Bạn vui lòng kiểm tra lại";
                                    break;
                                }
                            }
                            if (result)
                            {
                                msg.Bcc.Add(mailInfo.Bcc);
                            }
                        }
                        if (mailInfo.CC != null && mailInfo.CC != "" && result)
                        {
                            String[] ALL_EMAILS_CC = mailInfo.MailTo.Split(';');
                            foreach (string emailaddress in ALL_EMAILS_CC)
                            {
                                result = regex.IsMatch(emailaddress);
                                if (result == false)
                                {
                                    strMessage = "Địa chỉ email không hợp lệ. Bạn vui lòng kiểm tra lại";
                                    break;
                                }
                            }
                            if (result)
                            {
                                msg.CC.Add(mailInfo.CC);
                            }
                        }
                        if (result)
                        {
                            if (mailInfo.Attachment != null && mailInfo.Attachment != "")
                            {
                                Attachment attach = new Attachment(mailInfo.Attachment);
                                msg.Attachments.Add(attach);
                            }
                            msg.IsBodyHtml = true;

                            SmtpClient smtp = new SmtpClient();
                            if (!string.IsNullOrEmpty(mailInfo.HostMail))
                            {
                                smtp.Host = mailInfo.HostMail;
                            }
                            smtp.Port = mailInfo.Port;
                            smtp.Timeout = 15000;

                            smtp.EnableSsl = mailInfo.IsSSL;
                            if (mailInfo.IsSSL)
                                smtp.Credentials = new NetworkCredential(mailInfo.UserName, mailInfo.Password);

                            smtp.Send(msg);
                            strMessage = "Một Email đã được gửi đến: " + mailInfo.MailTo + ". Bạn vui lòng kiểm tra Email";
                        }

                    }
                    catch (SmtpException ex)
                    {
                        FsyLoggerHelper.WriteError("Fsy_MailController - SendMail() " + ex.Message);
                    }
                }
                return strMessage;
            }
            catch(Exception ex)
            {
                return ex.Message;
            }
        }      
    }
}
